<template>
	<view class="template-edit tn-safe-area-inset-bottom">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed alpha customBack>
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
				<text class='icon tn-icon-left'></text>
				<text class='icon tn-icon-home-capsule-fill'></text>
			</view>
		</tn-nav-bar>

		<view class="tn-safe-area-inset-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}" ref="form"
			:formData="model">


			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-my" style="font-size: 30rpx;"></text>
					</view>
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold">需求角色</view>
				</view>
				<tn-input v-model="model.role" type="select" placeholder="请选择角色" :border="border"
					:selectOpen="actionSheetShow" @click="actionSheetShow = true" inputAlign="right"></tn-input>
			</view>
			
			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-stop" style="font-size: 30rpx;"></text>
					</view>
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold">*截至时间</view>
				</view>
				<tn-input v-model="model.endTime" type="select" placeholder="请选择截至时间" :border="border"
					:selectOpen="timeShow" @click="timeShow = true" inputAlign="right"></tn-input>
			</view>
			
			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: A8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-refund" style="font-size: 30rpx;"></text>
					</view>
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold">指定接单人</view>
				</view>
				<tn-input v-model="model.people" type="select" placeholder="点击选择" :border="border"
					:selectOpen="peopleShow" @click="peopleShow = true" inputAlign="right"></tn-input>
			</view>
			
			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-time" style="font-size: 30rpx;"></text>
					</view>
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold">工时</view>
				</view>
				<tn-input placeholder="请输入工时" name="input" v-model="model.workDate" inputAlign="right"></tn-input>
			</view>
			
			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-refund" style="font-size: 30rpx;"></text>
					</view>
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold">*价格</view>
				</view>
				<tn-input placeholder="请输入单价(￥)" name="input" v-model="model.price" inputAlign="right"></tn-input>
			
			</view>
			
			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-tel-circle-fill" style="font-size: 30rpx;"></text>
					</view>
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold">*您的联系方式</view>
				</view>
				<tn-input placeholder="请填写联系方式" name="input" v-model="model.contactSend" inputAlign="right"></tn-input>
			
			</view>

			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-topics" style="font-size: 30rpx;"></text>
					</view>
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold">发布需求</view>
				</view>
				<view class="justify-content-item tn-text-df tn-color-grey">
					<text class="tn-padding-xs">300字内</text>
					<text class="tn-icon-keyboard-circle"></text>
				</view>
			</view>

			<view class="tn-margin tn-bg-gray--light" style="border-radius: 10rpx;padding: 20rpx 30rpx;">
				<input placeholder="请输入标题" name="input" placeholder-style="color:#AAAAAA" v-model="model.title"></input>
			</view>
			<view class="tn-margin tn-bg-gray--light tn-padding" style="border-radius: 10rpx;">
				<textarea maxlength="500" placeholder="请详细描述订单内容，如有任何纠纷客服会根据订单内容和实际接单情况进行综合判定，切勿发布违法违规订单！" v-model="model.content"
					placeholder-style="color:#AAAAAA"></textarea>
			</view>



	
		</view>

		<!-- 悬浮按钮-->
		<view class="tn-flex tn-footerfixed">
			<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
				<tn-button backgroundColor="#00FFC6" padding="40rpx 0" width="60%" shadow :loading="uploadBtn" fontBold
					@click="upload">
					<!-- <text class="tn-icon-light tn-padding-right-xs tn-color-black"></text> -->
					<text class="tn-color-black">发 布</text>
					<text class="tn-icon-camera tn-padding-left-xs tn-color-black"></text>
				</tn-button>
			</view>
		</view>



		<!-- <tn-action-sheet v-model="actionSheetShow" :list="listRole" @click="actionSheetClick"></tn-action-sheet> -->
		<tn-select ref="roleSelect" v-model="actionSheetShow" mode="single" title="选择角色" :list="listRole"
		 :searchShow="false" @confirm="actionSheetClick"  valueName="dictValue" labelName="dictLabel" >
		</tn-select>
		<tn-picker mode="time" v-model="timeShow" :params="params" @confirm="confirmPicker"></tn-picker>

		<tn-select ref="userSelect" v-model="peopleShow" mode="single" title="选择指定人" :list="listUser" :people="true"
			:searchShow="false" @confirm="confirmSelect" :searchPlaceholder="searchPlaceholder"
			@on-search-success="searchSuccess">
		</tn-select>

		<view class='tn-tabbar-height'></view>

	</view>
</template>

<script>
	import request from '../common/request';
	import {
		data
	} from '../tuniao-ui/libs/mixin/mixin';
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'

	export default {
		name: 'TemplateEdit',
		mixins: [template_page_mixin],
		data() {
			return {
				uploadBtn: false,
				actionSheetShow: false,
				timeShow: false,
				peopleShow: false,
				searchPlaceholder: "请输入用户唯一ID",
				listRole: [],
				listUser: [],
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				model: {
					role: '',
					title: '',
					content: '',
					endTime: '',
					workDate: '',
					price: '',
					people: '',
					appointPeople: '',
					contactSend: ''
				},
				orderTemp: {}
			}
		},

		mounted() {
			this.getDict()
		},
		onLoad: function(option) {
			if (JSON.stringify(option) !== '{}') {
				this.orderTemp = JSON.parse(option.info);
			}
		},
		methods: {
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			// 点击actionSheet选择角色
			actionSheetClick(event) {
				this.model.role = event[0].label;
			},
			// 点击确认按钮
			confirmPicker(event) {
				this.model.endTime = `${event.year}-${event.month}-${event.day}`
			},
			getDict() {
				console.log(this.orderTemp)
				if (JSON.stringify(this.orderTemp) !== '{}') {
					let temp = JSON.parse(this.orderTemp);
					this.model.title = temp.title
					this.model.content = temp.content
					this.model.endTime = temp.endTime
					this.model.workDate = temp.workDate
					this.model.price = temp.price
					this.model.people = temp.people
					this.model.appointPeople = temp.appointPeople
					this.model.contactSend = temp.contactSend
					this.model.role = temp.role
				}
				request.get("/system/dict/data/list", {
					dictType: 'xxc_role'
				}).then((res) => {
					this.listRole = res.rows;
				});
			},
			searchSuccess(e) {
				this.listUser = new Array();
				request.get("/WxOpenApi/getUserByIdOrName", {
					userId: e.detail.value
				}).then((res) => {
					this.listUser.push(res.data)
					setTimeout(() => this.$refs.userSelect.init());
					this.$refs.userSelect.searchPeopleResult(e.detail.value);
				});
			},
			confirmSelect(event) {
				console.log(event)
				this.model.people = event[0]['label']
				this.model.appointPeople = event[0]['value']
			},
			upload() {
				this.uploadBtn = true;
				if (this.model.title == '') {
					this.$t.message.toast('标题不能为空')
					return;
				}
				if (this.model.content == '') {
					this.$t.message.toast('内容不能为空')
					return;
				}
				if (this.model.role == '') {
					this.$t.message.toast('角色不能为空')
					return;
				}
				if (this.model.endTime == '') {
					this.$t.message.toast('截止不能为空')
					return;
				}
				if (this.model.price == '') {
					this.$t.message.toast('单价不能为空')
					return;
				}
				if(parseInt(this.model.price) == 0){
					this.$t.message.toast('单价不能为0')
					return;
				}
				let modelTemp = this.model;
				request.post("/xxc/orderMain/", this.model).then((res) => {
					if (res.msg == '余额不足！') {
						uni.showModal({
							title: '提示：',
							content: res.msg,
							success: function(res) {
								if (res.confirm) {
									uni.reLaunch({
										url: '/minePages/recharge?info=' + JSON.stringify(
											modelTemp)
									});
								}
							}
						});
					} else if (res.code == 500) {
						uni.showModal({
							title: '提示：',
							content: '系统异常',
							success: function(res) {
								if (res.confirm) {
									uni.reLaunch({
										url: '/pages/index'
									});
								}
							}
						});
					} else {
						uni.showModal({
							title: '提示：',
							content: '发布成功,等待审核通过',
							success: function(res) {
								if (res.confirm) {
									uni.reLaunch({
										url: '/pages/index'
									});
								}
							}
						});
					}
				});


			}

		}
	}
</script>

<style lang="scss" scoped>
	.template-edit {}

	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 100%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.7;
			background-color: #FFFFFF;
		}
	}

	/* 底部悬浮按钮 start*/
	.tn-tabbar-height {
		min-height: 100rpx;
		height: calc(120rpx + env(safe-area-inset-bottom) / 2);
	}

	.tn-footerfixed {
		position: fixed;
		width: 100%;
		bottom: calc(30rpx + env(safe-area-inset-bottom));
		z-index: 1024;
		box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0);

	}

	/* 底部悬浮按钮 end*/

	/* 标签内容 start*/
	.tn-tag-content {
		&__item {
			display: inline-block;
			line-height: 45rpx;
			padding: 10rpx 30rpx;
			margin: 20rpx 20rpx 5rpx 0rpx;

			&--prefix {
				padding-right: 10rpx;
			}
		}
	}

	/* 标签内容 end*/
</style>